/**
 * 
 */
package main;

import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.LazyOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import com.ccb.channel.sn.SnMapper;
import com.ccb.textinputformat.CustomStartEndDelimiter;

/**
 * @author congyang
 *
 */
public class Sn {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String input = "dedup_in\\sn\\sn.txt";
		String output = "dedup_out\\sn";
		runJob(input, output);
	}

	public static void runJob(String inpath, String outpath) throws Exception {
		
		long stime=new Date().getTime();
		Configuration conf = new Configuration();
		conf.set("textinputformat.record.startdelimiter", "{H:");
		conf.set("textinputformat.record.enddelimiter", "</Document>");

		Job job = Job.getInstance(conf, "Sn");
		job.setInputFormatClass(CustomStartEndDelimiter.class);
		//job.setOutputFormatClass(TextOutputFormat.class);
		LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);
		job.setJarByClass(Sn.class);
		job.setMapperClass(SnMapper.class);
		//job.setReducerClass(SnReducer.class);
		job.setNumReduceTasks(0);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		FileInputFormat.addInputPath(job, new Path(inpath));
		FileOutputFormat.setOutputPath(job, new Path(outpath));
		FileSystem.get(conf).delete(new Path(outpath), true);
		
		long etime = new Date().getTime();
		System.out.println((etime - stime) / 1000.0);
		
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

}
